package com.basin.micro.auth.module.menu.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.basin.common.core.consts.StatusConst;
import com.basin.micro.auth.module.menu.pojo.dto.MenuDTO;
import com.basin.micro.auth.module.menu.pojo.entity.MenuEntity;
import com.basin.micro.auth.module.menu.pojo.vo.MenuTreeVO;
import com.basin.micro.auth.module.menu.repository.MenuRepository;
import com.basin.micro.auth.module.menu.service.MenuService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: Sam ZHONG | sammy1997@aliyun.com
 */
@Service
@Slf4j(topic = "BASIN:ADMIN:MENU_SERVICE")
@RequiredArgsConstructor
public class MenuServiceImpl implements MenuService {
    private final MenuRepository menuRepository;


    @Override
    public List<MenuTreeVO> getMenuTree(String sysCode) {
        List<MenuDTO> menuList = menuRepository.list(Wrappers.<MenuEntity>lambdaQuery()
                        .eq(MenuEntity::getSysCode, sysCode)
                        .eq(MenuEntity::getMenuStatus, StatusConst.ENABLE)
                        .orderByAsc(MenuEntity::getMenuOrder))
                .stream()
                .map(MenuServiceImpl::getMenuDTO).toList();
        return MenuTreeVO.build(menuList);
    }

    private static MenuDTO getMenuDTO(MenuEntity entity) {
        return MenuDTO.builder()
                .menuType(entity.getMenuType().getCode())
                .routeType(entity.getMenuType().getCode())
                .menuStatus(entity.getMenuStatus())
                .menuRoute(entity.getMenuRoute())
                .menuIcon(entity.getMenuIcon())
                .menuOrder(entity.getMenuOrder())
                .menuName(entity.getMenuName())
                .menuCode(entity.getMenuCode())
                .parentCode(entity.getParentCode())
                .build();
    }
}
